package routers

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

type Login struct {
	Name     string `json:"name" form:"name" uri:"name" xml:"name" binding:"required"`
	Password string `json:"password" form:"password" uri:"password" xml:"password" binding:"required"`
}

func LoadLogin(engine *gin.Engine) {
	//engine.POST("/api/register", controllers.Register)

	engine.POST("/loginJson", func(ctx *gin.Context) {
		var json Login
		err := ctx.ShouldBindJSON(&json)
		if err != nil {
			ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
			return
		}
		if json.Name != "admin" || json.Password != "admin" {
			ctx.JSON(http.StatusBadRequest, gin.H{"status": "304"})
			return
		}
		ctx.JSON(http.StatusOK, gin.H{"status": "200", "info": "login success"})
	})
	engine.GET("/login", func(ctx *gin.Context) {
		ctx.HTML(http.StatusOK, "login.html", gin.H{})
	})
	engine.POST("/loginForm", func(ctx *gin.Context) {
		var form Login
		err := ctx.Bind(&form)
		if err != nil {
			ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
			return
		}
		if form.Name != "admin" || form.Password != "admin" {
			ctx.JSON(http.StatusBadRequest, gin.H{"status": "304"})
			return
		}
		ctx.JSON(http.StatusOK, gin.H{"status": "200"})
	})

	engine.GET("/:name/:password", func(ctx *gin.Context) {
		var login Login
		err := ctx.ShouldBindUri(&login)
		if err != nil {
			ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
			return
		}
		if login.Name != "admin" || login.Password != "admin" {
			ctx.JSON(http.StatusBadRequest, gin.H{"status": "304"})
			return
		}
		ctx.JSON(http.StatusOK, gin.H{"status": 200})
	})
}
